/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package util;

import util.tokenization_io.BoundarySymbols;


/**
 * A couple of utilities for reversed suffix arrays.
 */
public class Rev {

     
    /**
     * "lcs" here means "longest common suffix" not the usual
     * "longest common substring"
     * @param suftab
     * @param text
     * @param tc
     * @return 
     */
    public static int[] getLcstab(int[] suftab, int[] text, BoundarySymbols tc) {
        int[] result = new int[suftab.length];
        result[1] = -1;
        for (int i = 2; i < suftab.length; i++) {
            result[i] = lcs(suftab[i], suftab[i - 1], text, tc);
        }
        return result;
    }
    
    
    /**
     * "lcs" here means "longest common suffix" not the usual
     * "longest common substring" 
     * @param suftab
     * @param text
     * @param tc
     * @return 
     */
    public static int[] getLcstab0(int[] suftab, int[] text, BoundarySymbols tc) {
        int[] result = new int[suftab.length];
        result[0] = -1;
        for (int i = 1; i < suftab.length; i++) {
            result[i] = lcs(suftab[i], suftab[i - 1], text, tc);
        }
        return result;
    }

    /**
     * Longest common suffix
     * @param i
     * @param j
     * @param text
     * @param tc
     * @return 
     */
    public static int lcs(int i, int j, int[] text, BoundarySymbols tc) {
        int result = 0;
        while (text[i--] == text[j--] && !tc.userSentinel(text[i + 1])) {
            result++;
        }
        return result;
    }
}
